(function() {
var sw = window.screen.availWidth;
var sh = window.screen.availHeight;
var isIE = false && ~window.navigator.userAgent.indexOf('MSIE');

var starsTimeout = 15000;
var starsSpacing = 10000;
var starsNext = 0;

function loadStars() {
	var starsCalque = document.getElementById('stars');
	
	if (!starsCalque) {
		starsCalque = document.createElement('div');
		document.body.insertBefore(starsCalque,
			document.body.childNodes[0]);
		starsCalque.id = 'stars'; resizeStars();
	}
	
	var i = window.screen.availWidth *
			window.screen.availHeight;
	i = Math.floor(i / starsSpacing / 2);
	if (!isIE) i *= 2;
	
	starsNext = i; while (i--) {
		var star = document.createElement('span');
		star.style.background = 'url("images/nyan_star_' +
			(i % 10) + '.gif") 0 -100% no-repeat';
		starsCalque.appendChild(star); makeStar(1);
	}
}

function resizeStars() {
	var starsCalque = document.getElementById('stars');
	starsCalque.style.marginBottom = -starsCalque.offsetHeight + 'px';
}

function makeStar(center) {
	var starsCalque = document.getElementById('stars');
	var star = starsCalque.childNodes;
	if (++starsNext >= star.length) starsNext = 0;
	star = star[starsNext]; function roundom(max) {return (
			Math.floor(Math.random() * (max + 16) - 8));}
	var x = roundom(sw * 1.5), y = roundom(sh);
	var z = roundom(sw * 1.5); z = x - z - sw;
	if (isIE) x = Math.floor(x / 1.5);
	else if (!center) x += sw;else z -= sw;
	
	star.style.transitionDuration = '0ms';
	star.style.backgroundPosition = x + 'px ' + y + 'px';
	if (!isIE) setTimeout(function() {
		star.style.transitionDuration = starsTimeout + 'ms';
		star.style.backgroundPosition = z + 'px ' + y + 'px';
	setTimeout(makeStar, starsTimeout * x / (x - z));}, 1);
}

var onUse = window.attachEvent?'on':'';
var onEvt = onUse?'attachEvent':'addEventListener';
window[onEvt](onUse + 'load', loadStars);
window[onEvt](onUse + 'resize', resizeStars);
})();
